{extend name="common/index" /}

{block name="title"}<title>{$title}</title>{/block}

{block name="main"}
<div class="layui-fluid table">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    {$title}
                    {if (!empty($create_url))}
                    <div class="layui-input-inline pull-right">
                        <a data-href="{$create_url}" data-title="新增" admin-event="formLayer" refresh="" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary" style="margin-top:6px;">
                            新增
                        </a>
                    </div>
                    {/if}
                </div>
                <div class="layui-card-body admin-table">
                    <!--//搜索框-->
                    <div class="layui-row admin-table-action">
                        <div class="layui-col-md12 search-form">
                            <div class="layui-form layui-form-pane search layui-row">
                                <div class="layui-form-item layui-row ">
                                    {include file="common/base_search" /}

                                    <button lay-filter="search" lay-submit="" class="layui-btn  layui-btn-sm layui-btn-radius layui-btn-primary search-submit">
                                        筛选
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <span id="actuallyPaidAmount"></span>
                    <span id="actualCollectionAmount"></span>

                    <table id="table" lay-filter="table"></table>

                </div>
            </div>
        </div>
    </div>
</div>
{/block}

{block name="action"}
<script type="text/html" id="barDemo">
    {if (!empty($go_back_url))}
    {{#  if(d.pay_status == 2 && d.is_refund){ }}
        {{#  if(d.vip_class_id != 3 && d.status != 3){ }}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="update">退款</a>
        {{#  } }}
        {{#  if(d.pid == 0 && (d.vip_class_id ==1 || d.vip_class_id ==2 || d.vip_class_id ==5)){ }}
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="bindSale">绑定销售</a>
        {{#  } }}
    {{#  } }}
    {/if}
</script>
{/block}

{block name="footer"}
<script type="text/javascript">

    //初始化参数
    var cols = init_cols({$cols|json_encode|raw});
    var list_url = '{$list_url}';
    var edit_url = '{$go_back_url?:""}';
    var del_url = '{$del_url?:""}';
    var update_url = '{$go_back_url?:""}';
    var bind_url = '{$bind_url?:""}';
    var partition_id= '{$partition_id}';

    //初始化表格字段
    function init_cols(cols){
        var arr = [];
        cols.forEach((val) => {
            if(val.type == 'button'){
                val.templet = function (d) {
                    var str = '';
                    d.LAY_COL.option.forEach((v) => {
                        if(v[0] == d[d.LAY_COL['field']]){
                            var color = 'layui-btn-primary';
                            if(v[2] == ''){
                                color = '';
                            }else if(v[2] != undefined){
                                color = 'layui-btn-'+v[2];
                            }
                            str = '<button type="button" class="layui-btn layui-btn-xs '+color+'">'+v[1]+'</button>';
                        }
                    })
                    return str;
                }
            }else if(val.type == 'img'){
                val.templet = function (d) {
                    var str = '';
                    d[d.LAY_COL['field']].split(',').forEach((elem, index) => {
                        if(elem != ''){
                            str += '<img style="padding-left: 10px;" src="'+elem+'" class="view_cp" width="auto" height="100%">';
                        }
                    });
                    return str;
                }
            }
            arr.push(val);
        });
        return cols;
    }

    layui.config({
        base: '/vendor/layui-admin/layui/modules/js/' //静态资源所在路径
    });

    layui.define(['table','form','admin','laydate'], function(exports) {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var table_filter = {'partition_id':partition_id};
        table.render({
            elem: '#table'
            , url: list_url
            , cols: [cols]
            , page: true
            , limit: 20
            , height: 'full-180'
            ,toolbar: true //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            , text: {
                none: '无数据' //默认：无数据。
            }
            ,limits:[20,30,40,50,60,70,80,90,100,2000,5000]
            , even: true
            ,where: {
                filter:table_filter
            },
            done: function(res, curr, count){
                if (res.actuallyPaidAmount !== ''){
                    $("#actuallyPaidAmount").html('总支付金额：'+'￥'+res.actuallyPaidAmount+' 元');
                }
                if (res.actuallyPaidAmount !== ''){
                    $("#actualCollectionAmount").html('总实收金额：'+'￥'+res.actualCollectionAmount+' 元');
                }
            }
        });
        //tool 事件监听
        table.on('tool(table)', function(obj){
            var data = obj.data;
            if(obj.event === 'update') {

                layer.prompt({formType: 2,title: '退款操作（请输入10-200字）',value: ' ',area: ['800px', '350px']}, function (pass, index1) {
        
                    $.ajax({
                        url: update_url
                        , data: {"id": data.id, "transaction_id": pass,'partition_id':partition_id}
                        , type: 'POST'
                        , dataType: "json"
                        , success(res) {
                            if (res.code == 0) {
                                layer.close(index1);
                                table.reload("table");
                                layer.msg('处理完成');
                            } else {
                                layer.msg(res.msg);
                            }
                        }
                    });
                });
            }else if(obj.event === 'bindSale'){
                layer.open({
                    type: 2,
                    area: ['600px', '400px'],
                    offset: '40px',
                    content: ["/backmanage/member_vip_order/bindUser?id="+obj.data.id+"&partition_id="+partition_id]
                });
            }else{
                funcname = obj.event;
                window[funcname](data)
            }
        });
        //筛选框监听
        form.on('submit(search)', function(data){
            if (!data.field.partition_id){
                layer.msg('签名包不能为空');
                return false;
            }
            table_filter = data.field; //执行重载
            partition_id=data.field.partition_id
            table.reload('table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    filter:table_filter
                }
            });
            return false;
        });
        //重载 刷新
        $('#layui-icon-refresh').click(function () {
            reload();
        });
        //重载 刷新
        function reload(){
            table.reload('table',{
                where: {
                    filter:table_filter
                }
            });
        }
        exports('level', {});
        //日期时间范围
        laydate.render({
            elem: '#time_start'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#time_end'
            ,type: 'datetime'
        });
    });
</script>
{/block}